package com.youku.adSDK.view.components
{
   import flash.display.Sprite;
   import com.youku.adSDK.model.vo.ADData;
   import flash.display.SimpleButton;
   import flash.display.MovieClip;
   import flash.display.Loader;
   import com.youku.adSDK.ADContext;
   import com.youku.adSDK.ADEventDispather;
   import com.youku.utils.AssetUtil;
   import flash.events.MouseEvent;
   import com.youku.adSDK.view.events.ADEvent;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import flash.net.URLRequest;
   import com.youku.adSDK.view.events.FlashADEvent;
   import com.youku.adSDK.Utils.AdOpenWindow;
   import flash.net.sendToURL;
   
   public class OverlayAD extends Sprite
   {
      
      public function OverlayAD() {
         super();
      }
      
      private var _data:ADData;
      
      private var _playerWidth:Number;
      
      private var _playerHeight:Number;
      
      private var _barH:Number;
      
      private var _span:Number = 30;
      
      private var _panel:Sprite;
      
      private var _mask:Sprite;
      
      private var _content:Sprite;
      
      private var _closer:SimpleButton;
      
      private var _loaderContent:MovieClip;
      
      private var _loader:Loader;
      
      private var _clickSp:Sprite;
      
      private var _context:ADContext;
      
      private var _eventDispather:ADEventDispather;
      
      private var _endTime:Number;
      
      public function init(param1:ADData, param2:Number, param3:Number, param4:Number, param5:Number, param6:ADContext, param7:ADEventDispather) : void {
         this._context = param6;
         this._eventDispather = param7;
         this._data = param1;
         this._endTime = param2;
         this._playerWidth = param3;
         this._playerHeight = param4 - param5;
         this._barH = param5;
         if(this._data.CF == 3)
         {
            this.createFreeMode();
         }
         else
         {
            this.createFixMode();
         }
         this.addEvents();
         this.loadContent();
      }
      
      private function createFreeMode() : void {
         this._loader = new Loader();
         this.addChild(this._loader);
      }
      
      private function createFixMode() : void {
         this._panel = new Sprite();
         this.addChild(this._panel);
         this._mask = new Sprite();
         this._panel.addChild(this._mask);
         this.drawRect(this._mask);
         this._content = new Sprite();
         this._panel.addChild(this._content);
         this._content.mask = this._mask;
         this._closer = AssetUtil.getAsset("AD_closer") as SimpleButton;
         this._panel.addChild(this._closer);
         this._loader = new Loader();
         this._content.addChild(this._loader);
         this._clickSp = new Sprite();
         this.drawRect(this._clickSp,0);
         this._content.addChild(this._clickSp);
         this._closer.x = this._mask.width - this._closer.width - 2;
         this._closer.y = 2;
         this._closer.addEventListener(MouseEvent.MOUSE_UP,this.onCloserMouseUp);
         this._clickSp.buttonMode = true;
         this._clickSp.addEventListener(MouseEvent.CLICK,this.onClickSpClick);
         if(Number(this._data.CF) == 2 || Number(this._data.CF) == 3)
         {
            this._clickSp.visible = false;
         }
         this.layoutItems();
         this.visible = false;
      }
      
      private function addEvents() : void {
         this._eventDispather.addEventListener(ADEvent.CONTROL_BAR_HIDE,this.onBarHide);
         this._eventDispather.addEventListener(ADEvent.CONTROL_BAR_SHOW,this.onBarShow);
         this._eventDispather.addEventListener(ADEvent.CHANGE_SIZE,this.onPlayerResize);
      }
      
      private function loadContent() : void {
         this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onLoaderComplete);
         this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.onLoaderError);
         this._loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onLoaderError);
         var _loc1_:String = this._data.RS;
         if(Number(this._data.CF) > 1)
         {
            if(this._data.CU)
            {
               _loc1_ = _loc1_ + ("?overlay_cu=" + this._data.CU);
            }
            if(this._data.CUM)
            {
               _loc1_ = _loc1_ + ("&overlay_vcu=" + (this._data.CUM[0].U as String));
            }
         }
         if(this._context.isPaike == true)
         {
            _loc1_ = _loc1_ + ("?vid=" + String(this._context.videoIdUncode));
         }
         this._loader.load(new URLRequest(_loc1_));
      }
      
      private function onLoaderComplete(param1:Event) : void {
         this.visible = true;
         this.sendData(this._data.SUS);
      }
      
      private function onLoaderError(param1:Event) : void {
         this.dispatchEvent(new FlashADEvent(FlashADEvent.CONTENT_LOAD_ERROR));
      }
      
      private function onPlayerResize(param1:ADEvent) : void {
         this._playerWidth = param1.data.width;
         this._playerHeight = param1.data.height - param1.data.cheight;
         this._barH = param1.data.cheight;
         this.layoutItems();
      }
      
      private function layoutItems() : void {
         var _loc1_:* = NaN;
         var _loc2_:* = NaN;
         var _loc3_:* = NaN;
         var _loc4_:* = NaN;
         var _loc5_:* = NaN;
         if(this._panel)
         {
            _loc1_ = 1;
            if(this._playerWidth < this._mask.width + this._span * 2)
            {
               _loc1_ = (this._playerWidth - this._span * 2) / this._mask.width;
            }
            this._panel.scaleX = this._panel.scaleY = _loc1_;
            _loc2_ = 0;
            _loc3_ = 0;
            _loc4_ = this._mask.width * _loc1_;
            _loc5_ = this._mask.height * _loc1_;
            switch(this._data.LOC)
            {
               case "BOTTOM_LEFT":
                  _loc2_ = this._span;
                  _loc3_ = this._playerHeight - _loc5_ - this._span;
                  break;
               case "BOTTOM_CENTER":
                  _loc2_ = (this._playerWidth - _loc4_ * _loc1_) / 2;
                  _loc3_ = this._playerHeight - _loc5_ * _loc1_ - this._span;
                  break;
               case "BOTTOM_RIGHT":
                  _loc2_ = this._playerWidth - _loc4_ - this._span;
                  _loc3_ = this._playerHeight - _loc5_ - this._span;
                  break;
               case "TOP_LEFT":
                  _loc2_ = this._span;
                  _loc3_ = this._span;
                  break;
               case "TOP_CENTER":
                  _loc2_ = (this._playerWidth - _loc4_) / 2;
                  _loc3_ = this._span;
                  break;
               case "TOP_RIGHT":
                  _loc2_ = this._playerWidth - _loc4_ - this._span;
                  _loc3_ = this._span;
                  break;
               default:
                  _loc2_ = this._playerWidth - _loc4_ - this._span;
                  _loc3_ = this._playerHeight - _loc5_ - this._span;
            }
            this._panel.x = _loc2_;
            this._panel.y = _loc3_;
         }
      }
      
      private function onBarHide(param1:ADEvent) : void {
         if(this._panel)
         {
            this._playerHeight = this._playerHeight + this._barH;
            this.layoutItems();
         }
         else if(Number(this._data.CF) == 3 && (this._loader))
         {
            this._loader.y = this._barH;
         }
         
      }
      
      private function onBarShow(param1:ADEvent) : void {
         if(this._panel)
         {
            this._playerHeight = this._playerHeight - this._barH;
            this.layoutItems();
         }
         else if(Number(this._data.CF) == 3 && (this._loader))
         {
            this._loader.y = 0;
         }
         
      }
      
      private function onCloserMouseUp(param1:MouseEvent) : void {
         this.dispatchEvent(new FlashADEvent(FlashADEvent.CONTENT_CLOSE));
      }
      
      private function onClickSpClick(param1:MouseEvent) : void {
         if(!(this._data.CU == null) && !(this._data.CU == ""))
         {
            AdOpenWindow.open(new URLRequest(this._data.CU),"_blank");
         }
         this.sendData(this._data.CUM);
      }
      
      private function drawRect(param1:Sprite, param2:Number = 1) : void {
         param1.graphics.clear();
         param1.graphics.beginFill(0,param2);
         param1.graphics.drawRect(0,0,this._data.W,this._data.H);
         param1.graphics.endFill();
      }
      
      private function sendData(param1:Array) : void {
         var _loc2_:* = 0;
         var _loc3_:Object = null;
         var _loc4_:String = null;
         if((param1) && param1.length > 0)
         {
            _loc2_ = 0;
            while(_loc2_ < param1.length)
            {
               _loc3_ = param1[_loc2_] as Object;
               if((_loc3_) && (_loc3_.hasOwnProperty("U")))
               {
                  _loc4_ = param1[_loc2_].U as String;
                  if((_loc4_) && !(_loc4_.indexOf("http://") == -1))
                  {
                     sendToURL(new URLRequest(_loc4_));
                  }
               }
               _loc2_++;
            }
         }
      }
      
      public function show(param1:Boolean) : void {
         this.visible = param1;
      }
      
      public function dispose() : void {
         if((Number(this._data.CF) == 3) && (this._loader) && (this._loader.parent))
         {
            this.removeChild(this._loader);
         }
         else if((this._panel) && (this._panel.parent))
         {
            this.removeChild(this._panel);
         }
         
      }
      
      public function get endTime() : Number {
         return this._endTime;
      }
   }
}
